package com.qf.jie.controller;

import com.qf.jie.controller.servlet.BaseServlet;
import com.qf.jie.entity.po.User;
import com.qf.jie.entity.vo.RespBean;
import com.qf.jie.service.OrderService;
import com.qf.jie.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet("/OrderController")
public class OrderController extends BaseServlet {
    OrderService orderService = new OrderServiceImpl();

    //1.生成订单
    protected RespBean addOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String commodity_id = req.getParameter("commodity_id");
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        User user = (User) req.getSession().getAttribute("user");
        /**时间紧急，先在控制层判断*/
        if ( ! user.getPassword().equals(req.getParameter("password"))){
            return RespBean.respError("密码错误");
        }
        return orderService.addOrder(req, commodity_id, address, phone);
    }

    //2.获取订单（我是买家）
    protected RespBean getOrder_0(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        return orderService.getOrderListByUserId_0(req);
    }
    //3.获取订单（我是卖家）
    protected RespBean getOrder_1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        return orderService.getOrderListByUserId_1(req);
    }

    //4.发货
    protected RespBean Shipment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String order_id = req.getParameter("order_id");
        return orderService.updateStatusByUser_id_1(req, order_id);
    }
    //5.确认收货
    protected RespBean Receipt(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String order_id = req.getParameter("order_id");
        String commodity_id = req.getParameter("commodity_id");
        String user_id_0 = req.getParameter("user_id_0");
        String user_id_1 = req.getParameter("user_id_1");
        return orderService.updateStatusByUser_id_0(req,order_id, commodity_id, user_id_0,user_id_1);
    }


}
